package com.onlyphoto.onlymall.mvp.view.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.bean.AreaResult
import java.util.ArrayList



/**
 * Created by gaochujia on 2019-07-25.
 */

class RegionAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var list = arrayListOf<AreaResult>()
    var listener: OnSelectListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(
                R.layout.item_region, parent, false)
        return ViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    /*override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {
        holder as ViewHolder
    }*/

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as ViewHolder).loadData(list[position])
        holder.item.setOnClickListener{
            listener?.onSelected(position, list[position].id, list[position].name)
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var item: View = itemView
        var isSelected: ImageView? = null
        var regionName: TextView? = null

        init {
            isSelected = item.findViewById(R.id.is_selected)
            regionName = item.findViewById(R.id.region_name)
        }

        fun loadData(bean: AreaResult){
            regionName?.text = bean.name
            if (bean.isSelected) {
                isSelected?.visibility = View.VISIBLE
            } else {
                isSelected?.visibility = View.GONE
            }
        }
    }

    fun setData(list: ArrayList<AreaResult>) {
        this.list = list
        notifyDataSetChanged()
    }

    fun setOnSelectListener(listener: OnSelectListener){
        this.listener = listener
    }

    interface OnSelectListener{
        fun onSelected(position: Int, id: String, name: String)
    }
}